package com.gitee.cashzhang27.test.cloud.oauth.auth.server.extended.component;

import com.gitee.cashzhang27.test.cloud.oauth.auth.server.extended.entity.UserDetails;
import java.util.HashMap;
import java.util.Map;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

/**
 * 重写增强token方法,用于自定义一些token返回的信息
 *
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/02/11 16:15
 */
@Component
public class CustomTokenEnhancer implements TokenEnhancer {

  @Override
  public OAuth2AccessToken enhance(OAuth2AccessToken accessToken,
      OAuth2Authentication authentication) {
    UserDetails userDetailsVO = (UserDetails) authentication.getUserAuthentication()
        .getPrincipal();
    // 自定义一些token属性
    final Map<String, Object> additionalInformation = new HashMap<>(6);
    additionalInformation
        .put("user_id", userDetailsVO.getId());
    ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInformation);
    return accessToken;
  }
}

